import java.util.Scanner;

public class PasswordManage1 {
	//加密功
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("==============================");
            System.out.println("欢迎使用密码管理系统");
            System.out.println("==============================");
            System.out.println("请选择操作：");
            System.out.println("1. 加密");
            System.out.println("2. 解密");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 密码生成");
            System.out.println("5. 退出");
            System.out.print("请输入选项序号：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character

            switch (choice) {
                case 1:
                    System.out.print("请输入要加密的密码：");
                    String passwordToEncrypt = scanner.nextLine();
                    String encryptedPassword = encryptPassword(passwordToEncrypt);
                    System.out.println("加密后的结果是：" + encryptedPassword);
                    break;
                case 2:
                    System.out.print("请输入要解密的密码：");
                    String passwordToDecrypt = scanner.nextLine();
                    String decryptedPassword = decryptPassword(passwordToDecrypt);
                    System.out.println("解密后的结果是：" + decryptedPassword);
                    break;
                case 3:
                    System.out.print("请输入要判断强度的密码：");
                    String passwordToCheck = scanner.nextLine();
                    String passwordStrength = checkPasswordStrength(passwordToCheck);
                    System.out.println("密码强度：" + passwordStrength);
                    break;
                case 4:
                    System.out.print("请输入要生成密码的长度：");
                    int passwordLength = scanner.nextInt();
                    scanner.nextLine(); // Consume the newline character
                    String generatedPassword = generatePassword(passwordLength);
                    System.out.println("生成的密码是：" + generatedPassword);
                    break;
                case 5:
                    System.out.println("退出密码管理系统");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入");
                    break;
            }

            System.out.println("按任意键返回");
            scanner.nextLine(); // Wait for user input before continuing
        }
    }

    public static String encryptPassword(String password) {
        StringBuilder encryptedPassword = new StringBuilder();

        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            int ascii = (int) c + i +1+ 3;
            encryptedPassword.append((char) ascii);
        }

        StringBuilder reversedPassword = encryptedPassword.reverse();
        char firstChar = reversedPassword.charAt(0);
        char lastChar = reversedPassword.charAt(reversedPassword.length() - 1);
        reversedPassword.setCharAt(0, lastChar);
        reversedPassword.setCharAt(reversedPassword.length() - 1, firstChar);

        return reversedPassword.toString();

    }
    public static String decryptPassword(String encryptedPassword) {
        StringBuilder decryptedPassword = new StringBuilder(encryptedPassword);

        char firstChar = decryptedPassword.charAt(0);
        char lastChar = decryptedPassword.charAt(decryptedPassword.length() - 1);
        decryptedPassword.setCharAt(0, lastChar);
        decryptedPassword.setCharAt(decryptedPassword.length() - 1, firstChar);

        StringBuilder reversedPassword = decryptedPassword.reverse();

        for (int i = 0; i < reversedPassword.length(); i++) {
            char c = reversedPassword.charAt(i);
            int ascii = (int) c - i-1 - 3;
            reversedPassword.setCharAt(i, (char) ascii);
        }

        return reversedPassword.toString();
    }
    public static String checkPasswordStrength(String password) {
        int length = password.length();
        boolean hasDigit = false;
        boolean hasLowercase = false;
        boolean hasUppercase = false;

        for (int i = 0; i < length; i++) {
            char c = password.charAt(i);
            if (Character.isDigit(c)) {
                hasDigit = true;
            } else if (Character.isLowerCase(c)) {
                hasLowercase = true;
            } else if (Character.isUpperCase(c)) {
                hasUppercase = true;
            }
        }

        if (length < 8 || (length >= 8 && (!hasDigit || !hasLowercase && !hasUppercase))) {
            return "弱强度";
        } else if (length >= 8 && hasDigit && (hasLowercase || hasUppercase)) {
            return "中强度";
        } else if (length >= 8 && hasDigit && hasLowercase && hasUppercase) {
            return "高强度";
        }

        return "未知强度";
    }

    public static String generatePassword(int length) {
        StringBuilder password = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int randomType = (int) (Math.random() * 3);

            if (randomType == 0) {
                int randomDigit = (int) (Math.random() * 10);
                password.append(randomDigit);
            } else if (randomType == 1) {
                int randomLowercase = (int) (Math.random() * 26) + 97;
                password.append((char) randomLowercase);
            } else if (randomType == 2) {
                int randomUppercase = (int) (Math.random() * 26) + 65;
                password.append((char) randomUppercase);
            }
        }

        return password.toString();
    }
}
